<?php include('../variables/variables.php'); ?>

<?php

$con = mysql_connect($dbserver, $dbuser, $dbpass);
if ($con)
{
	mysql_select_db($db, $con);
	if (isset($_POST['inventory_id']) && isset($_POST['patron_id']))
	{
		$q = fquery($is_checked_in, array($_POST['inventory_id']));
		$result = mysql_query($q);
		if (mysql_num_rows($result) < 1)
		{
			mysql_close($con);
			header("location: /check_out.php?err=The book is already checked out");
		}
		else
		{
			session_start();
			$q = fquery($checkout_history, array($_POST['patron_id'], $_SESSION['id'], $_POST['inventory_id']));
			$result = mysql_query($q);
			if ($result)
			{
				$q = fquery($checkout_inventory, array($_POST['inventory_id']));
				$result = mysql_query($q);
				mysql_close($con);
				if ($result)
				{
					header("location: /check_out.php?msg=Book checked out successfully");
				}
				else
				{
					header("location: /check_out.php?err=Failed to checkout the book");
				}
			}
			else
			{
				mysql_close($con);
				header("location: /check_out.php?err=Failed to checkout the book");
			}
		}
	}
	else
	{
		mysql_close($con);
		header("location: /check_out.php?err=Please fill in all fields");
	}
}

?>
